<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select
        v-model="currentYear"
        size="small"
        style="width: 120px"
        @change="dateChange"
      >
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        >{{ item }}</el-option>
      </el-select>
      <el-select
        v-model="currentMonth"
        size="small"
        style="width: 120px; margin-left: 10px"
        @change="dateChange"
      >
        <el-option v-for="item in 12" :key="item" :label="item" :value="item">{{
          item
        }}</el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate" :first-day-of-week="7">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    // 日期过滤器
    getDay(value) {
      // 传进来的日期格式是2021-03-09
      // 转换为只剩下日的格式数据 取最后的两位数
      const day = value.split('-')[2]
      // startsWith()是判断开头字符的,0开头的去掉0
      return day.startsWith('0') ? day.substr(1) : day
    }
  },
  props: {
    // 定义起始日期,也可以有父组件传值决定
    startDate: {
      type: Date,
      // 默认当前日期
      default: () => new Date()
    }
  },
  data() {
    return {
      currentMonth: null, // 当前月份
      currentYear: null, // 当前年份
      currentDate: null,
      yearList: []
    }
  },
  created() {
    // 处理起始时间 要求起始时间必须是周一开始 以一个月为开始
    // getMonth()是从0开始算的
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentYear = this.startDate.getFullYear()
    // 创建一个长度为11值为空的数组Array(11)
    this.yearList = Array.from(
      Array(11),
      (value, index) => this.currentYear + index - 5
    )
    this.dateChange()
  },
  methods: {
    // 是否是休息日
    isWeek(value) {
      return value.getDay() === 6 || value.getDay() === 0
    },
    dateChange() {
      const year = this.currentYear
      const month = this.currentMonth
      const date = this.startDate.getDate()
      this.currentDate = new Date(`${year}-${month}-${date}`) // 以用户定的年月和默认的1号位开始日期
    }
  }
}
</script>

<style lang='scss' scoped>
::v-deep .el-calendar-day {
  height: auto;
}
::v-deep .el-calendar-table__row td,
::v-deep .el-calendar-table tr td:first-child,
::v-deep .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
  white-space: nowrap;
 position: relative;
}
.date-content .rest {
  position: absolute;
  left: 30px;
  top: 20px;
  color: #fff;
  border-radius: 50%;
  background: rgba(250, 77, 77, 0.501);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;

}
.date-content .text {
  width: 40px;
  height: 40px;
  line-height: 40px;
  display: inline-block;
}
::v-deep .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 25%;
}
::v-deep .el-calendar__header {
  display: none;
}
</style>
